const util = require('../../utils/util.js')
const amapFile = require('../../utils/amap-wx.js');
const app = getApp()
Page({
  data: {
    isLogin: false,
    spinShow: true,
    weatherData: '',
    bady: ''
  },
  onLoad: function () {
    const that = this
    that.setData({
      spinShow: true
    })
    app.login().then(res => {
      that.setData({
        isLogin: app.globalData.isLogin
      })
      if (app.globalData.isLogin) {
        that.getWeather()
        that.getbady()
      }
      setInterval(function () {
        if (that.data.isLogin == false || (that.data.weatherData != '' && that.data.bady != '')) {
          that.setData({
            spinShow: false
          })
        }
      }, 300)
    })
    setTimeout(function () {
      that.setData({
        spinShow: false
      })
    }, 1000)
  },
  onShow() {
    const that = this
    this.setData({
      isLogin: app.globalData.isLogin
    })
    if (app.globalData.isLogin == true) {
      if (this.data.weatherData == '') {
        wx.getStorage({
          key: 'weatherData',
          success(res) {},
          fail(res) {
            that.setData({
              spinShow: true
            })
            that.getWeather()
          },
          complete() {
            wx.getStorage({
              key: 'weatherData',
              success(res) {
                // console.log(res)
                that.setData({
                  weatherData: res.data
                })
              }
            })
            wx.getStorage({
              key: 'markers',
              success(res) {
                that.setData({
                  markers: res.data
                })
              }
            })
          }
        })
      }
      if (that.data.bady == '') {
        wx.getStorage({
          key: 'bady',
          success(res) {

          },
          fail(res) {
            that.setData({
              spinShow: true
            })
            that.getbady()
          },
          complete() {
            wx.getStorage({
              key: 'bady',
              success(res) {
                // console.log(res)
                that.setData({
                  bady: res.data
                })
              }
            })
          }
        })
      }
      setInterval(function () {
        that.getWeather()
        that.getbady()
      }, 30000)
    }
    setInterval(function () {
      if (that.data.isLogin == false || (that.data.weatherData != '' && that.data.bady != '')) {
        that.setData({
          spinShow: false
        })
      }
    }, 1000)
  },
  getUserProfile() {
    const that = this
    that.setData({
      spinShow: true
    })
    app.getUserProfile().then(res => {
      console.log(111)
      that.setData({
        isLogin: app.globalData.isLogin
      })
      this.getWeather()
      this.getbady()
    })
  },
  getWeather: util.throttle(function () {
    let that = this;
    let myAmapFun = new amapFile.AMapWX({
      key: '21166962ce01d2a58300da740b7d0af3'
    })
    myAmapFun.getWeather({
      success: function (res) {
        console.log(res)
        wx.getStorage({
          key: 'userLocation',
          success(res) {
            that.setData({
              markers: [{
                id: "map",
                latitude: Number(res.data.split(",")[1]).toFixed(4),
                longitude: Number(res.data.split(",")[0]).toFixed(4),
                width: 30,
                height: 30,
                iconPath: "../../icons/location.png"
              }]
            })
            wx.setStorage({
              data: [{
                id: "map",
                latitude: Number(res.data.split(",")[1]).toFixed(4),
                longitude: Number(res.data.split(",")[0]).toFixed(4),
                width: 30,
                height: 30,
                iconPath: "../../icons/location.png"
              }],
              key: 'markers',
            })
          }
        })
        that.setData({
          weatherData: res
        })
        wx.setStorage({
          data: that.data.weatherData,
          key: 'weatherData',
        })
      },
      fail: function (info) {
        console.log(info)
      }
    })
  }, 1000),
  /*获取婴儿车数据*/
  getbady: util.throttle(function () {
    // var that = this;
    // that.setData({
    //   bady: {
    //     wd: 36.1,
    //     zl: 1572,
    //     cs: "已上锁"
    //   }
    // });
    var that = this;
    wx.request({
      url: 'https://www.lewei50.com/api/V1/Gateway/GetSensors/02',
      data: {
        userkey: 'd8a8f914973c4fcabe28a98cd5b29600'
      },
      success: function (res) {
        console.log(res.data);
        wx.setStorage({
          data: {
            wd: res.data.Data[0].value,
            zl: res.data.Data[2].value,
            // cs: res.data.Data[3].value
            // cs: '已上锁'
          },
          key: 'bady',
        })
        that.setData({
          bady: {
            wd: res.data.Data[0].value,
            zl: res.data.Data[2].value,
            // cs: res.data.Data[3].value
            // cs: '已上锁'
          }
        })
      },
      fail: console.error
    })
  }, 1000),
  /*婴儿车开锁*/
  openclick: function () {
    var that = this;
    wx.request({
      url: 'https://www.lewei50.com/api/V1/Gateway/UpdateSensors/02',
      data: {
        Method: 'post',
        userkey: 'd8a8f914973c4fcabe28a98cd5b29600',
        Post: [{
          "Name": "K1",
          "Value": "打开"
        }]
      },
      success: function (res) {
        console.log(res.data);
      },
      fail: function (res) {}
    })
  },
  /*婴儿车锁车*/
  closeclick: function () {
    var that = this; //this不可以直接在wxAPI函数内部使用
    wx.request({
      url: 'https://www.lewei50.com/api/V1/Gateway/GetSensors/02',
      data: {
        userkey: 'd8a8f914973c4fcabe28a98cd5b29600'
      },
      success: function (res) {
        console.log(res.data);
      },
      fail: function (res) {
        ssss = "fail"
      }
    })
  },

});